var t = getApp(),
  a = t.requirejs("core"),
  s = t.requirejs("wxParse/wxParse");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    countDownDay: 0,
    countDownHour: 0,
    countDownMinute: 0,
    countDownSecond: 0,
    show_time: true,
    advWidth:0,
    icons: t.requirejs("icons"),
    display: false,
    isOn: false,
    n_animationData: '',
    news_num: 0, //未读消息
    get_phone: false,
    show_rec: 0, // 是否显示海报推荐人
    show_chat: 0, // 是否显示聊天
  },

  onGotUserInfo: function (q) {
    var that = this,
        cache = t.getCache("userinfo");

    // if (cache) {
    //   return;
    // }
    t.getUserInfo(q.detail.userInfo, function (t) {
      console.log("t",t);
      if (t != 1000) {
        var ucache = getApp().getCache("userinfo");
        if (ucache) {
          //getCurrentPages()[getCurrentPages().length - 1].onLoad();
          that.setData({
            display: false,
            getPhoneAuth: getApp().globalData.getphone
          })
        }
      } else {
        that.setData({
          display: true
        })
      }

    }, q.detail.encryptedData, q.detail.iv);

  },

  // 授权获取用户手机号
  getMobile: function (e) {
    t.globalData.getphone = false;
    this.setData({
      getPhoneAuth: false
    })
    t.getPhoneNumber(e, this, 3);
  },

  // 授权获取用户信息判断
  authCheck: function () {
    var cache = getApp().getCache("userinfo");
    if (!cache.nickName && !cache.avatarUrl) {
      this.setData({
        display: true
      })
      return false;
    } else {
      this.setData({
        display: false
      })
      return true;
    }
  },

  // 取消登录
  cancel_auth: function () {
    this.setData({
      display: false
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    var cache = t.getCache("userinfo");
    if (!cache.nickName && !cache.avatarUrl) {
      getApp().authSwitch(function(res){
        if (res == 1) {
          that.setData({
            display: true
          })
        } else {
          that.setData({
            display: false
          })
        }
      })
    }

    if (options.scene != undefined) {
      var scene = decodeURIComponent(options.scene);
          scene = scene.split(',');
      options.id = scene[0];
      options.pid = scene[1];
      options.card_id = scene[2];

      if (options.card_id != undefined) {
        t.globalData.card_id = options.card_id;
      }
    } else {
      if (options.card_id != undefined) {
        t.globalData.card_id = options.card_id;
      }
    }
    console.log("options",options)

    this.setData(options)
    this.detail()
    t.get_news_num(this);
    wx.getSystemInfo({//获取系统资料
      success: function (t) {
        console.log(t)
        that.setData({
          advWidth: t.windowWidth  //窗口宽度
        })
      }
    })
  },

  onShow: function () {
    this.hideModal();
  },

  to_chat: function() {
    a.jump('/yb_mingpian/pages/chats/chats');
  },

  // 聊天时获取用户手机号
  getPhoneNumber: function(e) {
    t.getPhoneNumber(e,this,1);
  },

  /**
   * 商品详情
   */
  detail: function () {
    var that = this,
        id = that.data.id;

    t.userlog(6, '秒杀商品', 'activity_browse', id);

    a.get('Miaosha/msGoodsDetail',{
      id: id,
      uid: getApp().getCache("userinfo").uid != undefined ? getApp().getCache("userinfo").uid : 0,
    },function(t){
      console.log("miaodetail",t)
      if(t.code==0){
        that.setData({
          info:t.info,
          show_chat: t.info.show_chat
        })
        wx.setNavigationBarTitle({
          title: t.info.name || "活动详情"
        })
        if (t.info.description){
          s.wxParse("wxParseData", "html", t.info.description, that, "0");
        }
        //倒计时
        if (t.info.status>0){
          if (t.info.status == 2) {
            var time = t.info.etime;
          } else if (t.info.status == 1) {
            var time = t.info.stime;
          }
          a.Countdown(time, function (i) {
            that.setData(i)
          })
        }
        
       
      }else{
        a.alert(t.msg,function(){
          return;
        });

      }
    },true);
    
  },
  

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.detail();
    wx.stopPullDownRefresh();
  },
  /**
   * 跳转至地图
   */
  navigate: function () {
    var that = this;
    var t = this.data.about_info;
    if (t.name && t.lat && t.lng) {
      a.tx_map(t.lat, t.lng, t.name)
    } else {
      a.toast('获取位置失败')
    }
  },

  /**
   * 直接购
   */
  shoping: function (e) {
    var auth_res = this.authCheck();
    if (!auth_res) {
      return;
    }

    var that = this,
      data = a.pdata(e);
    if (!that.data.show_time) {
      a.alert('该活动已经结束')
      return false;
    }
    if (that.data.info.rest_sell < 1) {
      a.alert('库存不足')
      return false;
    }
    wx.navigateTo({
      url: "/yb_mingpian/pages/miaosha/order/create/index?bargain_id=" + that.data.info.id + "&total=1&uid=" + getApp().getCache("userinfo").uid + "&activity_order_type=3&current_price=" + that.data.info.nprice + '&pic=' + that.data.info.pic + '&name=' + that.data.info.name + '&goods_id=' + that.data.info.goods_id
    })
  },
  handleCommonFormSubmit:function(){
    console.log('11')
  },
  /**
   * 打电话
   */
  phone: function (e) {
    a.phone(e)
  },

  // 转发分享
  onShareAppMessage: function () {
    this.hideModal();
    t.userlog(2, '秒杀商品', 'activity_relay');

    var user_name = getApp().getCache('userinfo').nickName != undefined ? getApp().getCache('userinfo').nickName : '';

    var title = user_name + "邀请您参加秒杀，秒杀价" + this.data.info.nprice+"元"+"-"+this.data.info.name; //this.data.info.name;
    var path = '/yb_mingpian/pages/miaosha/seckillGoods/index?id=' + this.data.info.id + '&card_id=' + t.globalData.card_id;
    console.log(path);
    return {
      title: title,
      path: path
    }
  },

  //底部弹窗设置
  show_Modal: function () {
    var that = this;

    a.get("poster/getConf", {
          type: 4
        }, function (res) {
          if (res.code == 0) {
            that.setData({
              show_rec: res.info.is_set
            })
          }

        var cache = getApp().getCache('userinfo');
        if (!cache.nickName && !cache.avatarUrl) {
          that.setData({
            show_rec: false
          })
        }

        // 显示遮罩层
        var animation = wx.createAnimation({
          duration: 200,
          timingFunction: "ease-in-out",
          delay: 0
        })
        that.animation = animation
        animation.translateY(500).step()
        that.setData({
          animationData: animation.export(),
          isOn: true
        })
        setTimeout(function () {
          animation.translateY(0).step()
          that.setData({
            animationData: animation.export()
          })
        }.bind(that), 200)
    })
  },

  hideModal: function () {
    this.setData({
      isOn: false,
    })
  }

})